<?php
	include_once("db/PayStation.inc");
	include_once("db/Ministry.inc");
	include_once("db/Department.inc");
	include_once("db/Province.inc");
	include_once("db/Region.inc");
	include_once("db/District.inc");
	
	
	$ministryId = $_REQUEST["ministry"];
	$departmentId = $_REQUEST["department"];
	$id = $_REQUEST["id"];
	
	$ps = PayStation::getPayStation($ministryId, $departmentId, $id);
	
	$ministry = Ministry::getMinistry($ministryId)->description;
	$department = Department::getDepartment($departmentId)->description;
?>

<script language="javascript"><!--
	function changeProvince() {
		var url = window.location.href;
		
		var ministry = <?php print $ministryId ?>;
		var department = <?php print $departmentId ?>;
		var id = <?php print $id ?>;
		var region = document.getElementById("region").value;
		var description = document.getElementById("description").value;		
		var province = document.getElementById("province").value;
	
		var url = "?action=editPayStation" +
			"&ministry=" + ministry +
			"&department=" + department + 
			"&id=" + id +
			"&region=" + region + 
			"&description=" + description + 
			"&province=" + province;
		
		window.location.href = url;
	}
	
	function checkLabels() {
		var regionModified = document.getElementById("region").value != <?php print $ps->regionCode ?>;
		var descModified = document.getElementById("description").value != "<?php print $ps->description ?>";
		var provModified = document.getElementById("province").value != <?php print $ps->provinceCode ?>;
		
		document.getElementById("regionLabel").innerHTML = (regionModified) ? "<b>Region**</b>" : "Region";
		document.getElementById("descLabel").innerHTML = (descModified) ? "<b>Description**</b>" : "Description";
		document.getElementById("provLabel").innerHTML = (provModified) ? "<b>Province**</b>" : "Province";
	}
--></script>

<h1>Edit Pay Station - <?php print $ps->payStationCode ?></h1>

<form method="post" action="?action=setPayStation">
	<input type="hidden" name="id" value="<?php print $ps->payStationCode ?>" />
	<input type="hidden" name="ministryId" value="<?php print $ps->ministryId ?>" />
	<input type="hidden" name="deptCode" value="<?php print $ps->deptCode ?>" />
	
	<table cellspacing="0" class="formLayout">
		<tr>
			<td>Code</td>
			<td><?php print $ps->payStationCode ?></td>
		</tr>
		<tr>
			<td>Ministry</td>
			<td><?php print $ministryId . " - " . $ministry ?></td>
		</tr>
		<tr>
			<td>Department</td>
			<td><?php print $departmentId . " - " . $department ?></td>
		</tr>
		<tr>
			<td><span id="regionLabel">Region</span></td>			
			<td><select id="region" name="region" onChange="checkLabels()">
				<?php
					$regionArray = Region::getAllRegions();
					
					$selectedRegion = $ps->regionCode;
					if (isset($_REQUEST["region"])) {
						$selectedRegion = $_REQUEST["region"];
					}
	
					foreach ($regionArray as $r) {
						print "<option value=\"$r->id\"";
						if ($r->id == $selectedRegion) print " selected=\"selected\"";
						print ">$r->id - $r->name</option>";
					}
				?>	
				</select>
			</td>
		</tr>
		<tr>
			<td><span id="descLabel" >Description</span></td>			
			<td><input type="text" id="description" name="description" onChange="checkLabels()"
				maxlength="40" value="<?php print (isset($_REQUEST["description"])) ? $_REQUEST["description"] : $ps->description ?>"/></td>
		</tr>
		<tr>
			<td><span id="provLabel">Province</span></td>
			<td>
				<select onChange="changeProvince()" id="province" name="province">
				<?php
					$prov = Province::getAllProvinces();
					
					$selectedProv = $ps->provinceCode;
					if (isset($_REQUEST["province"])) {
						$selectedProv = $_REQUEST["province"];
					}
	
					foreach ($prov as $p) {
						print "<option value=\"$p->id\"";
						if ($selectedProv == $p->id) print " selected=\"selected\"";		
						print ">$p->id - $p->name</option>";
					}
				?>	
				</select>
			</td>
		</tr>
		<tr>
			<td><span id="distLabel">District</span></td>
			<td><?php showDistrictDropDown($_REQUEST["province"], $ps->districtCode) ?></td>
		</tr>
		<tr colspan="2">
			<td>
				<input type="submit" value="Confirm" class="button"/>
				<input type="button" value="Cancel" class="button" onClick="window.location.href='?action=viewPayStations'" />
			</td>
		</tr>
	</table>
	
	<p><b>**</b> Indicates a modified field.</p>
	
	<script><!--
		checkLabels();
	--></script>
</form>